package com.ms.p2p.plugin.validation;


import com.ms.common.utils.WebErrors;
import com.ms.p2p.plugin.AutoRegisterPlugin;
import com.ms.p2p.plugin.validation.annotation.RequiredStringValidator;
import com.ms.p2p.plugin.validation.annotation.Validations;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

public class RequiredStringValidate extends AutoRegisterPlugin implements IValidate {

	@Override
	public WebErrors validate(HttpServletRequest request, WebErrors errors,
							  Validations validations, Map<String, String> decodedUriVariables) {
		RequiredStringValidator[] rsvs = validations.requiredStrings();
		for(RequiredStringValidator rsv : rsvs){
			String fieldName = rsv.fieldName();
			String value = request.getParameter(fieldName);
			if(StringUtils.isEmpty(value) && decodedUriVariables != null){
				value = decodedUriVariables.get(fieldName);
			}
			if(!rsv.trim() && StringUtils.isEmpty(value)
					|| rsv.trim() && StringUtils.isBlank(value)){
				errors.addErrorString(rsv.message());
			}
		}
		return errors;
	}

	@Override
	public String getType() {
		return "vallidate";
	}

	@Override
	public String getId() {
		return "requiredStringValidate";
	}

	@Override
	public String getName() {
		return "字符串非空校验";
	}

	@Override
	public String getVersion() {
		return "1.0.0";
	}

	@Override
	public String getAuthor() {
		return "fangchen";
	}

	@Override
	public void perform(Object... params) {
		
	}

	@Override
	public void register() {
		
	}
}
